package com.example.administrator.yuejian23.Fragment;


import  android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.yuejian23.Bean.Traindown;
import com.example.administrator.yuejian23.Model;
import com.example.administrator.yuejian23.R;
import com.example.administrator.yuejian23.Retrofit.RetrofitService;
import com.example.administrator.yuejian23.adapter.DegreeAdapter;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * A simple {@link Fragment} subclass.
 */
public class TrainSimpleFragment extends Fragment {

    private Context context;
    private List<Traindown> list;
    private DegreeAdapter adapter;
    private RecyclerView recyclerView;
    private View view;
    public TrainSimpleFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_train_simple, container, false);
        init();
        event();
        return view;
    }
    private void init() {
        recyclerView = (RecyclerView) view.findViewById(R.id.train_simple);
        context = getActivity();
    }

    public void  event() {
        Retrofit retrofit = Model.getConn();
        RetrofitService service = retrofit.create(RetrofitService.class);
        Call<List<Traindown>> call =service.getTrainDegreeList(1);
        call.enqueue(new Callback<List<Traindown>>() {
            @Override
            public void onResponse(Call<List<Traindown>> call, Response<List<Traindown>> response) {
                list = response.body();
                adapter = new DegreeAdapter(context, list);
                LinearLayoutManager manager = new LinearLayoutManager(context);
                manager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(manager);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<List<Traindown>> call, Throwable t) {
                Toast.makeText(context, "入门", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

